﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace _1.OrderService
{
    public partial class DeleteForm : Form
    {
        Form1 _form1;
        OrderService _orderService;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public DeleteForm(Form1 form1,OrderService orderService)
        {
            InitializeComponent();
            _form1 = form1;
            _orderService = orderService;
            this.SizeChanged += Form_SizeChanged;
        }

        private void DeleteForm_Load(object sender, EventArgs e)
        {
            asc.controllInitializeSize(this);
        }

        private void Form_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private bool IsNumber(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            string idString = textBox1.Text;
            if(!IsNumber(idString))
            {
                DialogResult dr = MessageBox.Show("订单单号输入不合法请重新输入", "警告", MessageBoxButtons.OK);
                textBox1.Text = "";
                return;
            }
            long orderID = Convert.ToInt64(idString);
            DialogResult dr2 = MessageBox.Show("是否删除此订单", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (dr2 == DialogResult.OK)
            {
                if(_orderService.deleteOrder(orderID))
                {
                    DialogResult dr = MessageBox.Show("订单删除成功", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    DialogResult dr = MessageBox.Show("不存在该订单 ，删除失败", "提示", MessageBoxButtons.OK);
                }               
            }
            textBox1.Text = "";
        }

        private void button_quit_Click(object sender, EventArgs e)
        {
            _form1.Show();
            this.Hide();
        }
    }
}
